package com.jackwoo.jerrymouse.http;

import com.jackwoo.jerrymouse.bin.Starter;
import com.jackwoo.jerrymouse.util.IOUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.*;
import java.net.http.HttpRequest;
import java.util.*;
import java.util.stream.Stream;

/**
 * Author: StupidZhe
 * Date: 2019/11/20
 * Desc: 抽象化HttpRequest
 **/
public class Request implements ServletRequest {

    private final static Logger logger = LoggerFactory.getLogger(Request.class);

    private String method;
    private String router;
    private String version;
    private String host;

    private Map<String, String> headers = new HashMap<>(10);
    private Map<String, Object> attributes = new HashMap<>(10);
    private Map<String, Object> parameters = new HashMap<>(10);


    private long contentLength;
    private byte[] bContents;



    public static Request parseRequest(InputStream stream) throws IOException {

        Request request = new Request();

        // 获取输入字节流
        request.bContents = IOUtil.getBytes(stream);
        String sContents = new String(request.bContents);

        request.contentLength = request.bContents.length;


        String[] arr = sContents.split("\n");
        if (Starter.debugMode)
            logger.debug("----get request------");
        if (arr.length <= 1) {
            return null;
        }
        for (int i = 0; i < arr.length; i++) {
            if (Starter.debugMode) {
                logger.debug(arr[i]);
            }

            if (i == 0) {
                String[] tmps = arr[i].split("[ ]");
                request.method = tmps[0];
                request.router = tmps[1];
                request.version = tmps[2];
            } else {
                String[] tmps = arr[i].split("[: ]");
                StringBuilder content = new StringBuilder();
                for (int n = 1; n < tmps.length; n++) {
                    content.append(tmps[n]);
                }
                request.headers.put(tmps[0], content.toString());
            }
        }
        if (Starter.debugMode)
            logger.debug("---------------------");

        return request;
    }

    public String getMethod() {
        return method;
    }

    public String getRouter() {
        return router;
    }

    public String getVersion() {
        return version;
    }

    public String getHost() {
        return host;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    @Override
    public Object getAttribute(String name) {
        return attributes.get(name);
    }

    @Override
    public Enumeration<String> getAttributeNames() {
        return Collections.enumeration(attributes.keySet());
    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public void setCharacterEncoding(String env) throws UnsupportedEncodingException {

    }

    @Override
    public int getContentLength() {
        return (int) contentLength;
    }

    @Override
    public long getContentLengthLong() {
        return contentLength;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
//        return new ByteArrayInputStream(bContents);
        return null;
    }

    @Override
    public String getParameter(String name) {
        return null;
    }

    @Override
    public Enumeration<String> getParameterNames() {
        return null;
    }

    @Override
    public String[] getParameterValues(String name) {
        return new String[0];
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return null;
    }

    @Override
    public String getProtocol() {
        return null;
    }

    @Override
    public String getScheme() {
        return null;
    }

    @Override
    public String getServerName() {
        return null;
    }

    @Override
    public int getServerPort() {
        return 0;
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return null;
    }

    @Override
    public String getRemoteAddr() {
        return null;
    }

    @Override
    public String getRemoteHost() {
        return null;
    }

    @Override
    public void setAttribute(String name, Object o) {
        attributes.put(name, o);
    }

    @Override
    public void removeAttribute(String name) {
        attributes.remove(name);
    }

    @Override
    public Locale getLocale() {
        return null;
    }

    @Override
    public Enumeration<Locale> getLocales() {
        return null;
    }

    @Override
    public boolean isSecure() {
        return false;
    }

    @Override
    public RequestDispatcher getRequestDispatcher(String path) {
        return null;
    }

    @Override
    public String getRealPath(String path) {
        return null;
    }

    @Override
    public int getRemotePort() {
        return 0;
    }

    @Override
    public String getLocalName() {
        return null;
    }

    @Override
    public String getLocalAddr() {
        return null;
    }

    @Override
    public int getLocalPort() {
        return 0;
    }

    @Override
    public ServletContext getServletContext() {
        return null;
    }

    @Override
    public AsyncContext startAsync() throws IllegalStateException {
        return null;
    }

    @Override
    public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException {
        return null;
    }

    @Override
    public boolean isAsyncStarted() {
        return false;
    }

    @Override
    public boolean isAsyncSupported() {
        return false;
    }

    @Override
    public AsyncContext getAsyncContext() {
        return null;
    }

    @Override
    public DispatcherType getDispatcherType() {
        return null;
    }
}
